const express = require('express');
const cors = require('cors');
const app = express();
const port = 8081;

app.use(cors());
app.use(express.json());

// Mock data
const classes = [
  { id: 1, name: '软件工程1班' },
  { id: 2, name: '软件工程2班' },
  { id: 3, name: '计算机科学1班' },
  { id: 4, name: '计算机科学2班' },
  { id: 5, name: '人工智能班' }
];

const courses = [
  { id: 1, name: '软件工程导论', class_name: '周一上午', description: '软件工程基础课程' },
  { id: 2, name: '数据结构', class_name: '周二下午', description: '计算机基础课程' },
  { id: 3, name: '算法分析', class_name: '周三上午', description: '计算机进阶课程' },
  { id: 4, name: '人工智能基础', class_name: '周四下午', description: 'AI入门课程' }
];

const students = [
  { id: 1, studentId: '20210001', name: '张三', classId: 1 },
  { id: 2, studentId: '20210002', name: '李四', classId: 1 },
  { id: 3, studentId: '20210003', name: '王五', classId: 2 },
  { id: 4, studentId: '20210004', name: '赵六', classId: 2 }
];

const signInStats = {
  totalTasks: 10,
  totalStudents: 120,
  signedIn: 98,
  absent: 22,
  signInRate: 81.67,
  classStats: [
    { classId: 1, className: '软件工程1班', total: 30, signedIn: 28, absent: 2, signInRate: 93.33 },
    { classId: 2, className: '软件工程2班', total: 32, signedIn: 25, absent: 7, signInRate: 78.13 },
    { classId: 3, className: '计算机科学1班', total: 29, signedIn: 24, absent: 5, signInRate: 82.76 },
    { classId: 4, className: '计算机科学2班', total: 29, signedIn: 21, absent: 8, signInRate: 72.41 }
  ]
};

// 学生签到任务
const signInTasks = [
  {
    id: 1,
    course_id: 1,
    course_name: "软件工程导论",
    description: "周一上午签到",
    start_time: Math.floor(Date.now() / 1000) - 3600, // 1小时前开始
    end_time: Math.floor(Date.now() / 1000) + 3600, // 1小时后结束
    created_at: "2023-06-20T08:30:00Z"
  },
  {
    id: 2,
    course_id: 2,
    course_name: "数据结构",
    description: "周二下午签到",
    start_time: Math.floor(Date.now() / 1000) - 1800, // 30分钟前开始
    end_time: Math.floor(Date.now() / 1000) + 1800, // 30分钟后结束
    created_at: "2023-06-20T10:30:00Z"
  }
];

// 签到记录
const signInRecords = [
  {
    id: 1,
    sign_in_task_id: 1,
    student_id: "20210001",
    course_id: 1,
    course_name: "软件工程导论",
    task_description: "周一上午签到",
    sign_in_time: Math.floor(Date.now() / 1000) - 1800,
    status: 1,
    location: "教学楼A101"
  },
  {
    id: 2,
    sign_in_task_id: 2,
    student_id: "20210001",
    course_id: 2,
    course_name: "数据结构",
    task_description: "周二下午签到",
    sign_in_time: Math.floor(Date.now() / 1000) - 900,
    status: 1,
    location: "教学楼B203"
  }
];

// API Routes

// 获取所有班级
app.get('/teacher/class/list', (req, res) => {
  res.json({ code: 0, msg: 'success', data: { classes } });
});

// 获取所有班级 (签到统计用)
app.get('/teacher/class/all', (req, res) => {
  res.json({ code: 0, msg: 'success', data: { classes } });
});

// 获取课程列表
app.get('/teacher/course/list', (req, res) => {
  res.json({ code: 0, msg: 'success', data: { courses } });
});

// 获取学生列表
app.get('/teacher/student/list', (req, res) => {
  const classId = req.query.class_id ? parseInt(req.query.class_id) : null;
  let filteredStudents = students;
  
  if (classId) {
    filteredStudents = students.filter(student => student.classId === classId);
  }
  
  res.json({ 
    code: 0, 
    msg: 'success', 
    data: { 
      students: filteredStudents,
      total: filteredStudents.length
    } 
  });
});

// 签到统计
app.get('/teacher/sign_in/statistics', (req, res) => {
  // 可以根据请求参数过滤数据
  const courseId = req.query.course_id;
  const classId = req.query.class_id;
  
  // 这里简单返回模拟数据，实际应用中可以根据参数过滤
  res.json({ code: 0, msg: 'success', data: signInStats });
});

// 学生端 API

// 学生登录
app.post('/student/login', (req, res) => {
  const { student_id, password } = req.body;
  
  // 简单验证
  if (student_id && password) {
    const student = students.find(s => s.studentId === student_id);
    
    if (student) {
      res.json({
        code: 200,
        message: "登录成功",
        data: {
          token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
          student: {
            id: student.id,
            student_id: student.studentId,
            name: student.name,
            avatar: null,
            created_at: "2023-06-20T08:30:00Z"
          }
        }
      });
    } else {
      res.status(400).json({
        code: 400,
        message: "学号或密码错误",
        data: null
      });
    }
  } else {
    res.status(400).json({
      code: 400,
      message: "请提供学号和密码",
      data: null
    });
  }
});

// 获取学生信息
app.get('/student/info', (req, res) => {
  // 从请求头获取token
  const token = req.headers.authorization?.split(' ')[1];
  
  if (token) {
    res.json({
      code: 200,
      message: "获取成功",
      data: {
        id: 1,
        student_id: "20210001",
        name: "张三",
        avatar: null,
        created_at: "2023-06-20T08:30:00Z"
      }
    });
  } else {
    res.status(401).json({
      code: 401,
      message: "未授权或会话已过期",
      data: null
    });
  }
});

// 获取待签到任务
app.get('/student/sign_in/tasks', (req, res) => {
  res.json({
    code: 200,
    message: "获取成功",
    data: signInTasks
  });
});

// 兼容旧API - 获取待签到任务
app.get('/student/sign_in/pending', (req, res) => {
  res.json({
    code: 200,
    message: "获取成功",
    data: signInTasks
  });
});

// 学生签到
app.post('/student/sign_in', (req, res) => {
  const { sign_in_task_id, face_image, location } = req.body;
  
  // 简单验证
  if (sign_in_task_id && face_image) {
    // 模拟人脸识别成功
    res.json({
      code: 200,
      message: "签到成功",
      data: {
        sign_in_id: signInRecords.length + 1,
        sign_in_time: Math.floor(Date.now() / 1000),
        status: 1
      }
    });
  } else {
    res.status(400).json({
      code: 400,
      message: "签到失败，请提供有效的人脸图像",
      data: null
    });
  }
});

// 兼容旧API - 学生签到
app.post('/student/sign_in/do', (req, res) => {
  const { sign_in_task_id, face_image, location } = req.body;
  
  // 简单验证
  if (sign_in_task_id && face_image) {
    // 模拟人脸识别成功
    res.json({
      code: 200,
      message: "签到成功",
      data: {
        sign_in_id: signInRecords.length + 1,
        sign_in_time: Math.floor(Date.now() / 1000),
        status: 1
      }
    });
  } else {
    res.status(400).json({
      code: 400,
      message: "签到失败，请提供有效的人脸图像",
      data: null
    });
  }
});

// 获取签到历史
app.get('/student/sign_in/history', (req, res) => {
  res.json({
    code: 200,
    message: "获取成功",
    data: signInRecords
  });
});

app.listen(port, () => {
  console.log(`Mock server running at http://localhost:${port}`);
}); 